﻿/// <reference path="../Core.js" />
/// <reference path="Panel.js" />
//"rgb(" + hong + "," + lv + "," + lan + ")"

IM.Control.SliderBarDirection = {
    //垂直
    Vertical: 1,
    //水平
    Horizontal: 2
};

IM.Control.SliderBar = function() {
    IM.Control.Panel.apply(this, arguments);
    this.Slider; //滑动块
    this.CompletedSide; //已经完成区
    this.Width; //宽
    this.Height; //高
    this.Direction = typeof (this.Direction) == "undefined" ? IM.Control.SliderBarDirection.Horizontal : this.Direction; //方向
    this.MaxValue = typeof (this.MaxValue) == "undefined" ? 100 : this.MaxValue; //最大值
    this.MinValue = typeof (this.MinValue) == "undefined" ? 0 : this.MinValue; //最小值
    this.Value = typeof (this.Value) == "undefined" ? 0 : this.Value; //当前值
    this.OnScroll = function() { document.title = this.Value; }; //滚动时

    //初始化
    this.SliderBarInit = function() {

        this.Slider = $("<div unselectable='on'>&nbsp;</div>");
        this.Slider.addClass("IM_Slider");
        this.CompletedSide = $("<div>&nbsp;</div>");
        this.CompletedSide.addClass("IM_CompletedSide");
        this.Render.append(this.CompletedSide);
        this.Render.append(this.Slider);

        switch (this.Direction) {
            case IM.Control.SliderBarDirection.Vertical:
                this.Render.addClass("IM_Vertical");
                this.Width = 4;
                this.Height = typeof this.Height == "undefined" ? 200 : this.Height;
                if (this.GetPosition() == "static")
                    this.Slider.css("left", parseInt(this.Render.offset().left) - 4);
                else
                    this.Slider.css("left", -4);
                this.CompletedSide.height((this.Height - this.Slider.height()) * this.Value / this.MaxValue);
                break;
            case IM.Control.SliderBarDirection.Horizontal:
                this.Render.addClass("IM_Horizontal");
                this.Width = typeof this.Width == "undefined" ? 200 : this.Width;
                this.Height = 4;
                if (this.GetPosition() == "static")
                    this.Slider.css("top", parseInt(this.Render.offset().top) - 4);
                else
                    this.Slider.css("top", -4);

                this.CompletedSide.width((this.Width - this.Slider.width()) * this.Value / this.MaxValue);
                break;
        }

        this.Render.width(this.Width);
        this.Render.height(this.Height);

        var startSelect = function() { return false; };



        IM.Mouse.Initial();

        var canMove = false;
        var offset = { X: 0, Y: 0 };
        //滚动方法
        var scrolling = (function(obj) {
            return function() {
                if (canMove) {
                    switch (obj.Direction) {
                        case IM.Control.SliderBarDirection.Vertical:
                            obj.CompletedSide.height(Math.min(Math.max((IM.Mouse.Location.Y - 5 - obj.Render.offset().top), 0), obj.Height - obj.Slider.height()));
                            if (obj.GetPosition() == "static")
                                obj.Slider.css("top", obj.CompletedSide.height() + obj.Render.offset().top);
                            else
                                obj.Slider.css("top", obj.CompletedSide.height());

                            obj.Value = obj.MaxValue * obj.CompletedSide.height() / (obj.Height - obj.Slider.height());
                            break;
                        case IM.Control.SliderBarDirection.Horizontal:
                            obj.CompletedSide.width(Math.min(Math.max((IM.Mouse.Location.X - 5 - obj.Render.offset().left), 0), obj.Width - obj.Slider.width()));
                            if (obj.GetPosition() == "static")
                                obj.Slider.css("left", obj.CompletedSide.width() + obj.Render.offset().left);
                            else
                                obj.Slider.css("left", obj.CompletedSide.width());

                            obj.Value = obj.MaxValue * obj.CompletedSide.width() / (obj.Width - obj.Slider.width());
                            break;
                    }
                    if (typeof obj.OnScroll != "undefined")
                        obj.OnScroll();
                }
            };
        })(this);

        var mousedown = (function(obj) {
            return function() {
                IM.AttachEvent(document.body, "onselectstart", startSelect, false);
                canMove = true;
                obj.Slider.addClass("IM_Slider_Down");
                $(document.body).bind("mousemove", scrolling).bind("mouseup", mouseup); ;

            };
        })(this);

        var mouseup = (function(obj) {
            return function() {
                if (canMove) {
                    IM.DetachEvent(document.body, "onselectstart", startSelect, false);
                    obj.Slider.removeClass("IM_Slider_Down");
                    canMove = false;
                    $(document.body).unbind("mousemove", scrolling);
                }
            };
        })(this);

        this.Slider.bind("mousedown", mousedown);

    };
    this.SliderBarInit();
};
